//
//  CompanyRecruitViewController.m
//  PASystem
//

//  企业招募

//

#import "CompanyRecruitViewController.h"
#import "CompanyRecruitModel.h"
#import "CompanyViewCell.h"

@interface CompanyRecruitViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UITableView *tab;

@property (nonatomic,assign) NSInteger currentPage;

@end

@implementation CompanyRecruitViewController

static NSString *const ID = @"CompanyRecruitCell";

-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    
    UITableView *tab1 = [[UITableView alloc] init];
    self.tab = tab1;
    tab1.delegate = self;
    tab1.dataSource = self;
    tab1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tab1];
    [tab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).mas_offset(10);
        make.left.equalTo(self.view.mas_left).mas_offset(10);
        make.right.equalTo(self.view.mas_right).mas_offset(-10);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-148);
    }];

    __weak typeof (self) weakSelf = self;
    
    [self.tab addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tab headerBeginRefreshing];
    
    [self.tab addFooterWithCallback:^{
        
        [weakSelf MoreData];
        
    }];

}

- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': qyzmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.ID,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        if (JSON) {
            [self.dataArray removeAllObjects];
            NSArray *arr = JSON[@"reason"];
            
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                
                CompanyRecruitModel *model = [CompanyRecruitModel mj_objectWithKeyValues:dict2];
                
                [self.dataArray addObject:model];
            }

        }
        
        
        [self.tab headerEndRefreshing];
        [self.tab reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (void)MoreData{

    self.currentPage ++;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': qyzmList,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,self.ID,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        if (JSON) {
            NSArray *arr = JSON[@"reason"];
            
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                
                CompanyRecruitModel *model = [CompanyRecruitModel mj_objectWithKeyValues:dict2];
                
                [self.dataArray addObject:model];
            }

        }
        
        [self.tab footerEndRefreshing];
        
        [self.tab reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CompanyRecruitModel *model = self.dataArray[indexPath.row];
    
    CompanyViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {
        
        cell = [CompanyViewCell WithCompanyViewCell];
    }
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 170;
}


@end


















